// Copyright (c) 2016 rickytan <ricky.tan.xin@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <objc/runtime.h>

#import "UIViewController+RTRootNavigationController.h"
#import "RTRootNavigationController.h"

@implementation UIViewController (RTRootNavigationController)
@dynamic rt_disableInteractivePop;


-(UIColor *)xm_backgroundColor{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setXm_backgroundColor:(UIColor *)backgroundColor{
    objc_setAssociatedObject(self, @selector(xm_backgroundColor), backgroundColor, OBJC_ASSOCIATION_ASSIGN);
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    UIImage * mainColorImage = [UIImage createImageWithColor:[UIColor xm_NavigationColor]];
    if (backgroundColor) {
//        mainColorImage = [UIImage createImageWithColor:backgroundColor];
//        UIColor *color1= XMColor(255, 70, 93);
//        UIColor *color2 = XMColor(251, 108, 42);
        UIColor *color1=[UIColor colorWithHexString:@"#FE942C" alpha:1];
        UIColor *color2=[UIColor colorWithHexString:@"#FB573D" alpha:1];

        NSArray* a=@[color1,color2];
        mainColorImage=[self gradientImageWithBounds:CGRectMake(0, 0, SCREEN_WIDTH, 1) andColors:a andGradientType:1];
        self.navigationController.navigationBar.shadowImage=mainColorImage;
//        self.navigationController.navigationBar.shadowImage = mainColorImage;
        
//        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//        UIColor *color1= XMColor(255, 70, 93);
//        UIColor *color2 = XMColor(251, 108, 42);
//        gradientLayer.colors = @[(__bridge id)color1.CGColor, (__bridge id)color2.CGColor];
//        gradientLayer.locations = @[@0.5, @1.0];
//        gradientLayer.startPoint = CGPointMake(0, 0);
//        gradientLayer.endPoint = CGPointMake(1.0, 0);
//        gradientLayer.bounds = CGRectMake(0, 0, SCREEN_WIDTH*2, 64+26);
//        [self.navigationController.navigationBar.layer addSublayer:gradientLayer];
    }
    [self.navigationController.navigationBar setBackgroundImage:mainColorImage forBarMetrics:UIBarMetricsDefault];
    
}

- (void)setRt_disableInteractivePop:(BOOL)rt_disableInteractivePop
{
    objc_setAssociatedObject(self, @selector(rt_disableInteractivePop), @(rt_disableInteractivePop), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)rt_disableInteractivePop
{
    return [objc_getAssociatedObject(self, @selector(rt_disableInteractivePop)) boolValue];
}

- (Class)rt_navigationBarClass
{
    return nil;
}

- (RTRootNavigationController *)rt_navigationController
{
    UIViewController *vc = self;
    while (vc && ![vc isKindOfClass:[RTRootNavigationController class]]) {
        vc = vc.navigationController;
    }
    return (RTRootNavigationController *)vc;
}

/**
 *  获取矩形的渐变色的UIImage(此函数还不够完善)
 *
 *  @param bounds       UIImage的bounds
 *  @param colors       渐变色数组，可以设置两种颜色
 *  @param gradientType 渐变的方式：0--->从上到下   1--->从左到右
 *
 *  @return 渐变色的UIImage
 */
- (UIImage*)gradientImageWithBounds:(CGRect)bounds andColors:(NSArray*)colors andGradientType:(int)gradientType{
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    
    switch (gradientType) {
        case 0:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(0.0, bounds.size.height);
            break;
        case 1:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(bounds.size.width, 0.0);
            break;
    }
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}


@end
